#include <sstream>
#include "gpio_m01.h"
#include "igslib_iutil.h"

namespace igslib
{
    gpio_m01::gpio_m01(const int& num):
        pin(nullptr)
    {
        pin = new gpio_syslib::pin(num);
    }
    gpio_m01::~gpio_m01()
    {
        if (pin != nullptr) delete pin;
    }
    int gpio_m01::SetMode(const mode& value)
    {
        mode old = dir;
        dir = value;
        try
        {
            if (dir == IGPIO::mode::input)
            {
                pin->set_direction(gpio_syslib::pin::direction::input);
            }
            else if (dir == IGPIO::mode::output)
            {
                pin->set_direction(gpio_syslib::pin::direction::output);
            }
        }
        catch(const std::exception&)
        {
            return -1;
        }
        if (old != value)
        {
            for(auto& each:events)
            {
                each->OnChangeMode(this,old,value);
            }
        }
        return 0;
    }
    int gpio_m01::SetState(const state& value)
    {
        state old = st;
        st = value;
        try
        {
            if (st == IGPIO::state::low)
            {
                pin->set_state(gpio_syslib::pin::state::low);
            }
            else if (st == IGPIO::state::high)
            {
                pin->set_state(gpio_syslib::pin::state::high);
            }
        }
        catch(const std::exception&)
        {
            return -1;
        }
        if (old != value)
        {
            for(auto& each:events)
            {
                each->OnChangeState(this,old,value);
            }
        }
        return 0;
    }
    IGPIO::mode gpio_m01::GetMode()
    {
        IGPIO::mode rtn = IGPIO::mode::undefined;
        
        try
        {
            if(pin->get_direction() == gpio_syslib::pin::direction::input)
            {
                rtn = IGPIO::mode::input;
            }
            else if(pin->get_direction() == gpio_syslib::pin::direction::input)
            {
                rtn = IGPIO::mode::output;
            }
        }
        catch(const std::exception&)
        {
        }
        return rtn;
    }
    IGPIO::state gpio_m01::GetState()
    {
        IGPIO::state rtn = IGPIO::state::undefined;
        try
        {
            if(pin->get_state() == gpio_syslib::pin::state::low)
            {
                rtn = IGPIO::state::low;
            }
            else if(pin->get_state() == gpio_syslib::pin::state::high)
            {
                rtn = IGPIO::state::high;
            }
        }
        catch(const std::exception&)
        {
        }
        return rtn;
    }
    std::string gpio_m01::GetPinName()
    {
        std::string rtn;
        std::stringstream ss;
        ss << pin->get_number() ;
        rtn = ss.str();
        return rtn;
    }
    std::string gpio_m01::GetName()
    {
        std::string rtn;
        std::stringstream ss("gpio_m01");
        ss << "(" << pin->get_number() << ")";
        rtn = ss.str();
        return rtn;
    }
};
